<?php

class objects_has_seasons_model extends MY_Model {

    function __construct() {
        parent::__construct();
        $this->table = 'objects_has_seasons';
    }

    // override
    public function find_all($sort = 'ID', $order = 'ASC') {
        $query = $this->db->get($this->table);
            return $query->result_array();
    }
    
    function object_seasons($object_id, $preview = NULL) {
        $q = "
            SELECT
            objects_has_seasons.objects_ID as objects_ID,
            objects_has_seasons.seasons_ID as seasons_ID,
            objects_has_seasons.active as seasons_active,
            seasons.name as seasons_name
            FROM objects_has_seasons
            LEFT JOIN seasons
            ON objects_has_seasons.seasons_ID = seasons.ID
            WHERE objects_has_seasons.objects_ID = $object_id
            ";

        if (!isset($preview))
            $q.="AND objects_has_seasons.seasons_ID != 1";

        $res = $this->db->query($q)->result_array();
        return $res;
    }
    
    function object_seasons_limit($object_id, $limit, $start, $query = NULL) {
        $q = "
            SELECT
            objects_has_seasons.objects_ID as objects_ID,
            objects_has_seasons.seasons_ID as seasons_ID,
            objects_has_seasons.active as seasons_active,
            seasons.name as seasons_name
            FROM objects_has_seasons
            LEFT JOIN seasons
            ON objects_has_seasons.seasons_ID = seasons.ID
            WHERE objects_has_seasons.objects_ID = $object_id
            ";

        if (!isset($preview))
            $q.="AND objects_has_seasons.seasons_ID != 1";
        
        if (isset($query))
            $q.=" AND seasons.name LIKE '%$query%'";

        $q .= " ORDER BY `seasons_ID` ASC LIMIT $limit, $start";

        $res = $this->db->query($q)->result_array();
        return $res;
    }

    function find_active_seasons($object_id) {
        $q = "
            SELECT *
            FROM objects_has_seasons
            WHERE objects_has_seasons.objects_ID = $object_id
            AND objects_has_seasons.active = 1
            ";

        $res = $this->db->query($q)->row_array();
        return $res['seasons_ID'];
    }

    function set_active_season($object_id, $season_ID) {
        // first set all active to 0
        $q = "
            UPDATE objects_has_seasons
            SET active = 0
            WHERE objects_ID = $object_id
            ";
        $this->db->query($q);

        $q = "
            UPDATE objects_has_seasons
            SET active = 1
            WHERE objects_ID = $object_id
            AND seasons_ID = $season_ID
            ";
        $res = $this->db->query($q);

        return $res;
    }

    function delete_object_season($object_id, $season_ID) {
        $q = "
            DELETE FROM
            objects_has_seasons
            WHERE objects_ID = $object_id
            AND
            seasons_ID = $season_ID
            ";

        $res = $this->db->query($q);
        return $res;
    }

    function delete_all($object_id) {
        $q = "
            DELETE FROM
            objects_has_seasons
            WHERE objects_ID = $object_id
            ";

        $res = $this->db->query($q);
        return $res;
    }

    function find_active_seasons_by_room($room_id) {
        $q = "

            SELECT objects_has_seasons.seasons_ID
            FROM objects_has_seasons
            LEFT JOIN objects ON objects_has_seasons.objects_ID  = objects.ID
            LEFT JOIN rooms ON objects.ID = rooms.objects_ID
            WHERE rooms.ID = ?
            AND objects_has_seasons.active = 1
            ";

        $res = $this->db->query($q, array($room_id))->row_array();
        return $res['seasons_ID'];
    }

}

?>
